#!/usr/bin/python
#-*-coding:utf-8-*-

import time, datetime, unittest
from base import *
from protocol import *
from httpDriver import *
import logConfig as log

#ISOTIMEFORMAT='%Y-%m-%d %X'

# 执行测试的类
class TestHttpPost(TestCase):
    def __init__(self, cpu):
        TestCase.__init__(self, cpu)
        self.cpu = cpu
        self.test_name = u"调度台"
        self.test_version = 'v1.0'
        self.test_pl = 'web'
        self.test_net = 'wifi'

        self.arg = {}
        self.arg["username"] = ''
        self.arg["password"] = ''
        self.arg["userType"] = ''
        self.arg["deviceId"] = ''
        self.arg["FN_KEY"] = 'AZ-FN-CODEC-KEY'
        self.arg["timestamp"] = getTimestamp()
        self.caseSet = [
            ('test_normal','test_abnormal_1','test_abnormal_2','test_abnormal_4','test_abnormal_5','test_abnormal_6')
        ]
        self.execTest()

    # 正常账号登录
    def test_normal(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("1", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'正常登录验证', u'检查账号密码正确且网络正常情况下是否能登录成功', u'登录的账号存在且网络正常', u'登录界面输入正确的账号密码，点击登录', u'能成功登录，界面会跳转到微型消防站统计界面','', u'缺陷', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("1", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'fail')

    # 异常请求链接的登录
    def test_abnormal_1(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.00.00.00", "80", "/fn/app/login.do"), -1)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('PASS:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("2",u'账号登录','post','http://114.00.00.00:80/fn/app/login.do','{"username":"fxs00001","password":"123456","userType":"1"}',"1000",response["content"],'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'访问IP异常的情况下是否能访问到', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("2", u'账号登录', 'post', 'http://114.00.00.00:80/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'fail')

    # 错误请求链接的登录
    def test_abnormal_2(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.10", "80", "/fn/app/login.do"), 0)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("3", u'账号登录', 'post', 'http://114.55.63.10:80/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'访问IP不正确的情况下是否能访问到', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山',u'Linyy')
            self.cpu["data"].testReportAppend("3", u'账号登录', 'post', 'http://114.55.63.10:80/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'fail')

    # 错误端口请求的登录
    def test_abnormal_3(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "8000", "/fn/app/login.do"), -1)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("4", u'账号登录', 'post', 'http://114.55.63.81:8000/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'访问端口不正确的情况下是否能访问到', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("4", u'账号登录', 'post', 'http://114.55.63.81:8000/fn/app/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'fail')

    # 请求到了错误的文件
    def test_abnormal_4(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "login.do"), 0)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("5", u'账号登录', 'post', 'http://114.55.63.81:80/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'访问文件目录不正确的情况下是否能访问到', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("5", u'账号登录', 'post', 'http://114.55.63.81:80/login.do', '{"username":"fxs00001","password":"123456","userType":"1"}', "1000", response["content"], 'fail')

    # 密码前加入空格的登录
    def test_abnormal_5(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = ' 123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('P.','T')
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("6", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":" 123456","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'密码前加空格的情况下的登录请求', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("6", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":" 123456","userType":"1"}', "1000", response["content"], 'fail')

    # 错误密码登录
    def test_abnormal_6(self):
        self.startTime = time.time()
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123450'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        response = self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)
        self.endTime = time.time()
        self.useTime = self.endTime - self.startTime
        if response["status"] == 1:
            self.test_success += 1
            log.TestLog('=======================================', 'T')
            log.TestLog('CASE:         '+sys._getframe().f_code.co_name+"  ("+__name__+")",'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'T')
            log.TestLog('----------------------------------------------------------------------', 'T')
            self.cpu["data"].testReportAppend("7", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":"123450","userType":"1"}', "1000", response["content"], 'success')
        else:
            self.test_failed += 1
            log.TestLog('=======================================','F')
            log.TestLog('FAIL:          '+sys._getframe().f_code.co_name+"  ("+__name__+")",'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            log.TestLog('RESULT:     '+response["content"],'F')
            log.TestLog('USETIME:   '+'%.3f seconds'%self.useTime,'F')
            log.TestLog('----------------------------------------------------------------------', 'F')
            self.cpu["data"].testResultAppend(u'账号登录', u'异常登录验证', u'错误密码情况下的登录请求', u'登录的账号存在且网络正常', u'向异常的链接进行访问', u'不能成功登录','', u'优化', u'一般', u'WZ-1.0-02-06-1', u'范训山', u'Linyy')
            self.cpu["data"].testReportAppend("7", u'账号登录', 'post', 'http://114.55.63.81:80/fn/app/login.do', '{"username":"fxs00001","password":"123450","userType":"1"}', "1000", response["content"], 'fail')

if __name__ == "__main__":
    suite = unittest.TestSuite()
    runner = unittest.TextTestRunner()
    runner.run(suite)

